package com.lancoo.answer.helper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.entity.TaskControlBean;

/**
 * Created by pengmi on 2021/12/9 19:50
 * Description:提交确认弹窗
 */
public class SubmitConfirmDialogHelper {

    private static long subTime;

    /**
     * 作答提交确认弹窗
     * @param mContext context
     * @param unAnswerCount 未作答小题
     * @param callBack callback
     * @return 1
     */

    public static AlertDialog showConfirmDialog(Context mContext, int unAnswerCount, final SubmitConfirCallBack callBack){
        View inflate=LayoutInflater.from(mContext).inflate(R.layout.ev_dialog_switch_answer_type,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(mContext,R.style.ev_dialogStyle);
        builder.setView(inflate);
        builder.setCancelable(false);
        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        if(window==null){
            return alertDialog;
        }
        WindowManager.LayoutParams attributes = window.getAttributes();
        int width =mContext.getResources().getDisplayMetrics().widthPixels;
        attributes.width=Math.min(width*3/4,width*3/4);
        alertDialog.getWindow().setAttributes(attributes);
        TextView tv_cancel = inflate.findViewById(R.id.tv_cancel);
        TextView tv_sure = inflate.findViewById(R.id.tv_sure);
        TextView tv_content = inflate.findViewById(R.id.tv_content);
        ImageView img_logo = inflate.findViewById(R.id.img_logo);
        img_logo.setImageResource(R.drawable.ev_vector_prompt_blue);
        tv_cancel.setTextColor(0xff0099ff);
        tv_cancel.setText(R.string.ev_cancel);
        tv_sure.setText(R.string.ev_sure);
        if(unAnswerCount==0){
            tv_content.setText("您确定提交作答结果吗?");
        }
        tv_content.setText("还有试题未作答，是否继续提交?");
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if(taskControlBean!=null){
            boolean hp = taskControlBean.isHp();
            if(hp){
                tv_content.setText("还有试题未互评，是否继续提交?");
            }
        }
        inflate.findViewById(R.id.tv_cancel).setOnClickListener(v -> {
            if(System.currentTimeMillis()-subTime<200){
                return;
            }
            subTime=System.currentTimeMillis();
            alertDialog.dismiss();
            callBack.onCancel();
        });
        inflate.findViewById(R.id.tv_sure).setOnClickListener(v -> {
            if(System.currentTimeMillis()-subTime<200){
                return;
            }
            subTime=System.currentTimeMillis();
            alertDialog.dismiss();
            callBack.onSure();
        });
        return alertDialog;
    }

    public abstract static class SubmitConfirCallBack{

        public void onCancel(){}

        public void onSure(){}

    }

}
